package com.person.yhzs.wxPaySDK.service.impl;

import com.person.yhzs.wxPaySDK.WXPay;
import com.person.yhzs.wxPaySDK.WXPayConfig;
import com.person.yhzs.wxPaySDK.WXPayConstants;
import com.person.yhzs.wxPaySDK.WXPayUtil;
import com.person.yhzs.wxPayV3.common.WechatConstants;
import com.person.yhzs.wxPaySDK.config.MyWXPayConfig;
import com.person.yhzs.wxPaySDK.service.WXPayService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author luolin
 * @Description
 * @createTime 2022年06月28日 14:19:00
 */
public class WXPayServiceImpl implements WXPayService {

    @Resource
    private WechatConstants wechatConstants;

    @Override
    public Map<String, String> wxPay(String openId, String ip) throws Exception {
        String appid = wechatConstants.getAPPID();
        String mch_key_v3 = wechatConstants.getMCH_KEY_V3();
        // 1、拼接统一下单地址参数
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("body","测试商品名称");  // 商家名称-销售商品类目、String(128)
        paramMap.put("openid",openId);  // openId，通过登录获取
        paramMap.put("out_trade_no", UUID.randomUUID().toString().replaceAll("-",""));  // 订单号，每次不同
        paramMap.put("spbill_create_ip",ip);
        paramMap.put("total_fee","1");  // 支付金额，单位分，即0.01元
        paramMap.put("trade_type","JSAPI");  // 支付类型
        paramMap.put("sign_type", WXPayConstants.MD5);    // 签名方式
        System.out.println("paramMap为：" + paramMap);
        System.out.println("=========== 分割线 ========");
        // 2、通过MyWXPayConfig创建WXPay对象，用于支付请求
        final String SUCCESS_NOTIFY = "https://luolin.vip/api/wxPay/success";   // 支付成功后，通知的接口
        boolean useSandbox = false; // 是否使用沙盒环境支付API，是的话不会真正扣钱
        WXPayConfig config = new MyWXPayConfig();
        WXPay wxPay = new WXPay(config,SUCCESS_NOTIFY,false,useSandbox);

        // 3、fillRequestData会将上述参数用key=value形式和mchKey一起加密为sign，验证地址：https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=20_1
        Map<String, String> map = wxPay.unifiedOrder(wxPay.fillRequestData(paramMap));

        // 4、发送post请求“统一下单接口”（https://api.mch.weixin.qq.com/pay/unifiedorder）,返回预支付id:prepay_id
        String prepayId = map.get("prepay_id");
        System.out.println("xmlStr为：" + map);
        System.out.println("============== 分割线 ================");

        HashMap<String, String> payMap = new HashMap<String,String>();
        payMap.put("appId", appid);
        payMap.put("timeStamp", WXPayUtil.getCurrentTimestamp() + "");
        payMap.put("nonceStr",WXPayUtil.generateNonceStr());
        if(useSandbox){
            payMap.put("signType", WXPayConstants.MD5);
        }else{
            payMap.put("signType",WXPayConstants.MD5);
        }
        payMap.put("package","prepay_id=" + prepayId);

        // 5、通过appId,timeStamp, nonceStr, signType, package及商户密钥进行key=value形式拼接并加密
        String paySign = null;
        if(useSandbox){
            paySign = WXPayUtil.generateSignature(payMap,mch_key_v3,WXPayConstants.SignType.MD5);
        }else{
            paySign = WXPayUtil.generateSignature(payMap,mch_key_v3,WXPayConstants.SignType.MD5);
        }
        payMap.put("paySign",paySign);

        //6、将这6个参数传给前端
        System.out.println("payMap:" + payMap);

        return payMap;
    }
}
